package com.dragon.client.controller.avttendance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.avttendance.ITaskService;
import com.dragon.repository.domain.dto.TaskDTO;
import com.dragon.repository.domain.vo.TaskVO;
import com.dragon.repository.entity.Task;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-14
 */
@RestController
@RequestMapping("task")
@Api(tags = "任务管理")
public class TaskController {

    @Autowired
    private ITaskService service;

    @GetMapping("list")
    @ApiOperation("获取任务列表")
    public ResponseData<List<TaskDTO>> selectList(Integer page, Integer limit, String search){
        Page<Task> taskPage = service.taskList(page, limit, search);
        return ResponseData.success(TaskDTO.convertList(taskPage.getRecords()));
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public ResponseData addTask(@RequestBody @Validated(Insert.class)TaskVO vo){
        service.addTask(TaskVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public ResponseData updateTask(@RequestBody @Validated(Update.class) TaskVO vo){
        service.updateTask(TaskVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public ResponseData deleteTask(Integer id){
        service.deleteTask(id);
        return ResponseData.success();
    }

}
